﻿Ext.define('app.prm.document.Trash', {
    extend: 'Ext.panel.Panel',
    layout: 'border',
    projectId: 0,
    controller: 'document',
    requires: ['app.prm.document.DocumentController', 'app.prm.document.ViewModel'],
    viewModel: {
        type: 'documentModel'
    },
    initComponent: function () {
        var me = this;
        me.projectId = me.paramsId;

        me.items = [
            me.getTree(),
            me.getGrid()
        ];
        me.callParent();
    },
    getTree: function () {
        var me = this;
        if (!this.tree) {
            this.tree = new Ext.tree.Panel({
                border: false,
                region: 'west',
                width: 320,
                title: '文档类型',
                split: true,
                useArrows: true,
                rootVisible: false,
                columns: [{
                    xtype: 'treecolumn',
                    text: '名称',
                    width: 250,
                    sortable: true,
                    dataIndex: 'text'
                }, {
                    text: '删除个数',
                    dataIndex: 'trashNum',
                    sortable: false,
                    renderer: function (value, p, record) {
                        if (record.data.id != -1 && !record.data.leaf) {
                            return "";
                        }
                        return value;
                    }
                }],
                store: new Ext.data.TreeStore({
                    root: {
                        expanded: true
                    },
                    autoLoad: true,
                    nodeParam: 'parentId',
                    // 根节点的参数值是0
                    defaultRootId: "0",
                    proxy: {
                        type: 'ajax',
                        reader: {
                            type: 'json',
                            rootProperty: function (node) {
                                return node.result || node.children;
                            }
                        },
                        url: '/api/services/app/document/GetProjectDocumentTree?projectId=' + me.projectId
                    }
                }),
                root: { id: '0', text: 'text' },
                listeners: {
                    afterrender: function (tree) { tree.expandAll(); },
                    select: function (tree, record, index, eOpts) {
                        if (record.data.leaf || record.data.id == -1) {
                            var typeId = record.data.id;
                            if (record.data.id == -1) {
                                typeId = -1;
                            }
                            var id = record.get('id');
                            var stepname = "";

                            var __index = id.indexOf('_');
                            var __length = id.length;
                            if (__index > 0) {
                                stepname = id.substring(__index + 1);
                                typeId = id.substring(0, __index);
                            }
                            console.log(id + '>>' + stepname);

                            me.getGrid().store.load({ params: { typeId: typeId, stepname: stepname, projectId: me.projectId, isDelete: true } });
                        }
                    }
                }
            });
        }
        return this.tree;
    },
    getGrid: function () {
        var me = this;
        if (!this.grid) {
            var gridStore = Ext.create("app.ux.PagePrmStore", { url: '/api/services/app/document/GetDocument', autoLoad: false });

            this.gridbbar = Ext.create({
                xtype: 'pagingtoolbar',
                store: gridStore,   // same store GridPanel is using
                dock: 'bottom',
                displayInfo: true
            });
            this.grid = new Ext.grid.Panel({
                title: '文档列表',
                region: 'center',
                border: false,
                viewConfig: {
                    enableTextSelection: true //可以复制单元格文字
                },
                forceFit: true,
                store: gridStore,
                columns: [
                    {
                        header: "名称", dataIndex: 'name', width: 200
                    },
                    { header: "大小(kb)", dataIndex: 'length' },
                    { header: "类型", dataIndex: 'extension' },
                    { header: "版本", dataIndex: 'versionNo' },
                    {
                        header: "创建时间", dataIndex: 'creationTime', renderer: function (value, p, record) {
                            return Ext.util.Format.date(value, 'Y-m-d');
                        }
                    },
                    {
                        header: "创建人", dataIndex: 'creationUser'
                    },
                    {
                        xtype: 'actioncolumn',
                        width: 70,
                        header: '操作',
                        items: [
                            {
                                iconCls: 'bookopen',
                                tooltip: '查看版本',
                                handler: function (grid, rowIndex, colIndex) {
                                    var record = grid.store.getAt(rowIndex);
                                    if (!record.data.hasViewVersion) {
                                        return false;
                                    }
                                    var win = Ext.create("app.prm.document.Version", { documentId: record.data.id });
                                    win.show();
                                },
                                getClass: function (v, meta, record) {
                                    if (record.data.hasViewVersion) {
                                        return 'bookopen';
                                    } else {
                                        return 'x-hidden';
                                    }
                                }
                            },
                            {
                                iconCls: 'download',
                                tooltip: '下载',
                                handler: function (grid, rowIndex, colIndex) {
                                    var record = grid.store.getAt(rowIndex);
                                    if (!record.data.hasDownLoad) {
                                        return false;
                                    }
                                    window.location.href = '/api/services/app/document/GetFile?path=' + record.data.fullUrl;
                                    //App.AjaxGet('/api/services/app/document/GetFile?path=' + record.data.fullUrl, function ()
                                    //{

                                    //});
                                },
                                getClass: function (v, meta, record) {
                                    if (record.data.hasDownLoad) {
                                        return 'download';
                                    } else {
                                        return 'x-hidden';
                                    }
                                }
                            }
                        ]
                    }
                ],
                dockedItems: [this.gridbbar]
            });
        }
        return this.grid;
    },
    loadGrid: function () {
        var me = this;
        var node = me.getTree().getSelection()[0];
        //var isdelete = me.lookupReference('txt_check').getValue();
        me.getGrid().store.load({ params: { typeId: node.data.id, projectId: me.projectId, isDelete: true } });
    }
});